package chan.project.smartlawlawyer.service.impl;

import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.constant.CommonConstant;
import chan.project.smartlawcommon.exception.ThrowUtils;
import chan.project.smartlawcommon.utils.SqlUtils;
import chan.project.smartlawlawyer.mapper.LawyerInfoMapper;
import chan.project.smartlawlawyer.service.LawyerInfoService;
import chan.project.smartlawmodel.dto.lawyer.LawyerInfoQueryRequest;
import chan.project.smartlawmodel.entity.LawyerInfo;
import chan.project.smartlawmodel.entity.UserInfo;
import chan.project.smartlawmodel.vo.LawyerInfoVO;
import chan.project.smartlawmodel.vo.UserInfoVO;
import chan.project.smartlawservice.userService.UserFeignClient;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**
 * 律师服务实现
 *
 * @author <a href="https://github.com/kavansp">kavansp</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Service
@Slf4j
public class LawyerInfoServiceImpl extends ServiceImpl<LawyerInfoMapper, LawyerInfo> implements LawyerInfoService {

    @Resource
    private UserFeignClient userService;

    /**
     * 校验数据
     *
     * @param lawyerInfo
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validLawyerInfo(LawyerInfo lawyerInfo, boolean add) {
        ThrowUtils.throwIf(lawyerInfo == null, ErrorCode.PARAMS_ERROR);
        //  从对象中取值
        String name = lawyerInfo.getName();
        String lawFirm = lawyerInfo.getLawFirm();
        String speciallyArea = lawyerInfo.getSpeciallyArea();
        String introduction = lawyerInfo.getIntroduction();
        // 创建数据时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(name), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(StringUtils.isBlank(lawFirm), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(StringUtils.isBlank(speciallyArea), ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        if (StringUtils.isNotBlank(introduction)) {
            ThrowUtils.throwIf(introduction.length() < 1000, ErrorCode.PARAMS_ERROR, "自我介绍过长");
        }
    }

    /**
     * 获取查询条件
     *
     * @param lawyerInfoQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<LawyerInfo> getQueryWrapper(LawyerInfoQueryRequest lawyerInfoQueryRequest) {
        QueryWrapper<LawyerInfo> queryWrapper = new QueryWrapper<>();
        if (lawyerInfoQueryRequest == null) {
            return queryWrapper;
        }
        // 从对象中取值
        Long id = lawyerInfoQueryRequest.getId();
        Long notId = lawyerInfoQueryRequest.getNotId();
        String sortField = lawyerInfoQueryRequest.getSortField();
        String sortOrder = lawyerInfoQueryRequest.getSortOrder();
        Long userId = lawyerInfoQueryRequest.getUserId();
        String name = lawyerInfoQueryRequest.getName();
        String lawFirm = lawyerInfoQueryRequest.getLawFirm();
        String speciallyArea = lawyerInfoQueryRequest.getSpeciallyArea();
        String introduction = lawyerInfoQueryRequest.getIntroduction();

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(introduction), "introduction", introduction);
        queryWrapper.like(StringUtils.isNotBlank(lawFirm), "lawFirm", lawFirm);
        queryWrapper.like(StringUtils.isNotBlank(lawFirm), "speciallyArea", speciallyArea);

        // 精确查询
        queryWrapper.ne(ObjectUtils.isNotEmpty(notId), "id", notId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(name), "name", name);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取律师封装
     *
     * @param lawyerInfo
     * @param request
     * @return
     */
    @Override
    public LawyerInfoVO getLawyerInfoVO(LawyerInfo lawyerInfo, HttpServletRequest request) {
        // 对象转封装类
        LawyerInfoVO lawyerInfoVO = LawyerInfoVO.objToVo(lawyerInfo);
        // 关联查询用户信息
        Long userId = lawyerInfo.getUserId();
        UserInfo user = null;
        if (userId != null && userId > 0) {
            user = userService.getById(userId);
        }
        UserInfoVO userVO = UserInfoVO.objToVo(user);
        lawyerInfoVO.setUser(userVO);
        return lawyerInfoVO;
    }

    /**
     * 分页获取律师封装
     *
     * @param lawyerInfoPage
     * @param request
     * @return
     */
    @Override
    public Page<LawyerInfoVO> getLawyerInfoVOPage(Page<LawyerInfo> lawyerInfoPage, HttpServletRequest request) {
        List<LawyerInfo> lawyerInfoList = lawyerInfoPage.getRecords();
        Page<LawyerInfoVO> lawyerInfoVOPage = new Page<>(lawyerInfoPage.getCurrent(), lawyerInfoPage.getSize(), lawyerInfoPage.getTotal());
        if (CollUtil.isEmpty(lawyerInfoList)) {
            return lawyerInfoVOPage;
        }
        // 对象列表 => 封装对象列表
        List<LawyerInfoVO> lawyerInfoVOList = lawyerInfoList.stream().map(LawyerInfoVO::objToVo).collect(Collectors.toList());
        lawyerInfoVOPage.setRecords(lawyerInfoVOList);
        return lawyerInfoVOPage;
    }

}
